import { ColumnOption, SqlGenerator } from './sql-generator';

export class MysqlSqlGenerator implements SqlGenerator {
  createTable(
    tableName: string,
    columns: ColumnOption[],
    options: { engine?: string; charset?: string; comment?: string } = {},
  ): string {
    options.engine = options.engine || 'InnoDB';
    options.charset = options.charset || 'utf8mb4';
    const cols: string[] = [];

    for (const c of columns) {
      const text = `${c.name} ${c.type} ${c.isAutoIncrement ? 'AUTO_INCREMENT' : ''} ${c.nullable ? 'NULL' : 'NOT NULL'} ${c.primary ? 'PRIMARY KEY' : ''} ${c.default ? `DEFAULT ${c.default}` : ''}`;
      cols.push(text);
    }

    let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (  
          ${cols.join(',\n')}
        ) ENGINE=${options.engine} DEFAULT CHARSET=${options.charset} COMMENT='${options.comment}';
        `;

    return sql;
  }

  dropTable(tableName: string): string {
    return `DROP TABLE IF EXISTS ${tableName};`;
  }
}
